package imy.virtual.touch;

import android.app.ActivityManager;
import android.app.Instrumentation;
import android.content.Context;
import android.os.Process;
import android.util.Log;

import androidx.annotation.NonNull;

import java.util.List;

public class TouchCore {
    private static TouchCore gCore = new TouchCore();
    private static final String TAG = "TouchCore";

    public static TouchCore get() {
        return gCore;
    }
    private Instrumentation instrumentation;
    public void star(Context context, Instrumentation instrumentation) {
        String processName = getProcessName(context);
        String mainProcess = context.getApplicationInfo().processName;
        //执行shell命令
        this.instrumentation =instrumentation;
        if (mainProcess.equals(processName)) {
            Log.i(TAG, "TouchCore初始化");

            //
        }
    }

    public Instrumentation getInstrumentation() {
        return instrumentation;
    }

    private String getProcessName(@NonNull Context context) {
        int pid = Process.myPid();
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (am != null) {
            List<ActivityManager.RunningAppProcessInfo> runningAppList = am.getRunningAppProcesses();
            if (runningAppList != null) {
                for (ActivityManager.RunningAppProcessInfo processInfo : runningAppList) {
                    if (processInfo.pid == pid) {
                        return processInfo.processName;
                    }
                }
            }
        }
        return null;
    }
}
